package src.ten;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Phonebook extends JFrame {

    JPanel contentPane;
    JTable table;

    public Phonebook(){
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowActivated(WindowEvent e) {
               do_this_windowActivated(e);
            }
        });
        setTitle("\u6211\u7684\u7535\u8BDD\u7C3F");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100,100,450,250);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5,5,5,5));
        contentPane.setLayout(new BorderLayout(0,0));
        setContentPane(contentPane);

        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane,BorderLayout.CENTER);

        table = new JTable();
        scrollPane.setViewportView(table);
    }

    protected void do_this_windowActivated(WindowEvent e){
        Map<String,String> directory = new HashMap<String,String>();
        directory.put("阿一","33265****");
        directory.put("阿二","89562****");
        directory.put("阿三","56232****");
        directory.put("阿四","12545****");
        directory.put("阿五","89565****");
        directory.put("阿六","45862****");
        directory.put("阿七","78566****");
        directory.put("阿八","89565****");
        directory.put("阿九","98566****");
        directory.put("阿十","19565****");
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setColumnIdentifiers(new Object[]{"姓名","手机"});
        Set<String> names = directory.keySet();
        for (Iterator<String> it = names.iterator();it.hasNext();){
            String name = it.next();
            model.addRow(new Object[]{name,directory.get(name)});
        }
    }

}
